<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Command-module for cursor movement and editing &mdash; Dragonfly-modules v0.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Dragonfly-modules v0.1 documentation" href="index.html" />
    <link rel="next" title="Command-module for command memory" href="mod-_cmdmemory.html" />
    <link rel="prev" title="Command-module for moving and controlling windows" href="mod-_winctrl.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_cmdmemory.html" title="Command-module for command memory"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-_winctrl.html" title="Command-module for moving and controlling windows"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  
  <div class="section" id="command-module-for-cursor-movement-and-editing">
<h1>Command-module for cursor movement and <strong>editing</strong><a class="headerlink" href="#command-module-for-cursor-movement-and-editing" title="Permalink to this headline">¶</a></h1>
<p>This module allows the user to control the cursor and
efficiently perform multiple text editing actions within a
single phrase.</p>
<div class="section" id="example-commands">
<h2>Example commands<a class="headerlink" href="#example-commands" title="Permalink to this headline">¶</a></h2>
<p><em>Note the &#8220;/&#8221; characters in the examples below are simply
to help the reader see the different parts of each voice
command.  They are not present in the actual command and
should not be spoken.</em></p>
<dl class="docutils">
<dt>Example: <strong>&#8220;up 4 / down 1 page / home / space 2&#8221;</strong></dt>
<dd>This command will move the cursor up 4 lines, down 1 page,
move to the beginning of the line, and then insert 2 spaces.</dd>
<dt>Example: <strong>&#8220;left 7 words / backspace 3 / insert hello Cap world&#8221;</strong></dt>
<dd>This command will move the cursor left 7 words, then delete
the 3 characters before the cursor, and finally insert
the text &#8220;hello World&#8221;.</dd>
<dt>Example: <strong>&#8220;home / space 4 / down / 43 times&#8221;</strong></dt>
<dd>This command will insert 4 spaces at the beginning of
of this and the next 42 lines.  The final &#8220;43 times&#8221;
repeats everything in front of it that many times.</dd>
</dl>
</div>
<div class="section" id="discussion-of-this-module">
<h2>Discussion of this module<a class="headerlink" href="#discussion-of-this-module" title="Permalink to this headline">¶</a></h2>
<p>This command-module creates a powerful voice command for
editing and cursor movement.  This command&#8217;s structure can
be represented by the following simplified language model:</p>
<blockquote>
<ul>
<li><dl class="first docutils">
<dt><em>CommandRule</em> &#8211; top-level rule which the user can say</dt>
<dd><ul class="first last">
<li><dl class="first docutils">
<dt><em>repetition</em> &#8211; sequence of actions (name = &#8220;sequence&#8221;)</dt>
<dd><ul class="first last simple">
<li><em>KeystrokeRule</em> &#8211; rule that maps a single
spoken-form to an action</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><em>optional</em> &#8211; optional specification of repeat count</dt>
<dd><ul class="first last simple">
<li><em>integer</em> &#8211; repeat count (name = &#8220;n&#8221;)</li>
<li><em>literal</em> &#8211; &#8220;times&#8221;</li>
</ul>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
</li>
</ul>
</blockquote>
<p>The top-level command rule has a callback method which is
called when this voice command is recognized.  The logic
within this callback is very simple:</p>
<ol class="arabic simple">
<li>Retrieve the sequence of actions from the element with
the name &#8220;sequence&#8221;.</li>
<li>Retrieve the repeat count from the element with the name
&#8220;n&#8221;.</li>
<li>Execute the actions the specified number of times.</li>
</ol>
</div>
<div class="section" id="configuration-examples">
<h2>Configuration examples<a class="headerlink" href="#configuration-examples" title="Permalink to this headline">¶</a></h2>
<div class="section" id="configuration-multiedit-en-txt">
<h3>Configuration &#8220;_multiedit-en.txt&#8221;<a class="headerlink" href="#configuration-multiedit-en-txt" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># This is a config file for Dragonfly&#39;s _multiedit.py command-module.</span>
<span class="c">#  To use this config, you must rename this file to _multiedit.txt and</span>
<span class="c">#  place it in the same directory as the _multiedit.py file.</span>
<span class="c">#</span>

<span class="c"># Pull in all of Dragonfly&#39;s action objects so that we can use them here.</span>

<span class="kn">from</span> <span class="nn">dragonfly</span> <span class="kn">import</span> <span class="o">*</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we define the release action which releases all</span>
<span class="c">#  modifier-keys used within this grammar.  It is defined here</span>
<span class="c">#  because this functionality is used in many different places.</span>
<span class="c">#  Note that it is harmless to release (&quot;...:up&quot;) a key multiple</span>
<span class="c">#  times or when that key is not held down at all.</span>

<span class="n">release</span> <span class="o">=</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;shift:up, ctrl:up&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we define the single-action commands.  These can be spoken</span>
<span class="c">#  in series so as to execute multiple actions within a single utterance.</span>

<span class="n">cmd</span><span class="o">.</span><span class="n">map</span> <span class="o">=</span> <span class="p">{</span>
           <span class="c"># Spoken-form    -&gt;    -&gt;    -&gt;     Action object</span>

           <span class="s">&quot;up [&lt;n&gt;]&quot;</span><span class="p">:</span>                         <span class="n">Key</span><span class="p">(</span><span class="s">&quot;up:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;down [&lt;n&gt;]&quot;</span><span class="p">:</span>                       <span class="n">Key</span><span class="p">(</span><span class="s">&quot;down:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;left [&lt;n&gt;]&quot;</span><span class="p">:</span>                       <span class="n">Key</span><span class="p">(</span><span class="s">&quot;left:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;right [&lt;n&gt;]&quot;</span><span class="p">:</span>                      <span class="n">Key</span><span class="p">(</span><span class="s">&quot;right:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;page up [&lt;n&gt;]&quot;</span><span class="p">:</span>                    <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgup:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;page down [&lt;n&gt;]&quot;</span><span class="p">:</span>                  <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgdown:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;up &lt;n&gt; (page | pages)&quot;</span><span class="p">:</span>            <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgup:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;down &lt;n&gt; (page | pages)&quot;</span><span class="p">:</span>          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgdown:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;left &lt;n&gt; (word | words)&quot;</span><span class="p">:</span>          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-left:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;right &lt;n&gt; (word | words)&quot;</span><span class="p">:</span>         <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-right:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;home&quot;</span><span class="p">:</span>                             <span class="n">Key</span><span class="p">(</span><span class="s">&quot;home&quot;</span><span class="p">),</span>
           <span class="s">&quot;end&quot;</span><span class="p">:</span>                              <span class="n">Key</span><span class="p">(</span><span class="s">&quot;end&quot;</span><span class="p">),</span>
           <span class="s">&quot;doc home&quot;</span><span class="p">:</span>                         <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-home&quot;</span><span class="p">),</span>
           <span class="s">&quot;doc end&quot;</span><span class="p">:</span>                          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-end&quot;</span><span class="p">),</span>

           <span class="s">&quot;space [&lt;n&gt;]&quot;</span><span class="p">:</span>                      <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;space:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;enter [&lt;n&gt;]&quot;</span><span class="p">:</span>                      <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;enter:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;tab [&lt;n&gt;]&quot;</span><span class="p">:</span>                        <span class="n">Key</span><span class="p">(</span><span class="s">&quot;tab:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;delete [&lt;n&gt;]&quot;</span><span class="p">:</span>                     <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;del:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;delete [&lt;n&gt; | this] (line|lines)&quot;</span><span class="p">:</span> <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;home, s-down:</span><span class="si">%(n)d</span><span class="s">, del&quot;</span><span class="p">),</span>
           <span class="s">&quot;backspace [&lt;n&gt;]&quot;</span><span class="p">:</span>                  <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;backspace:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;pop up&quot;</span><span class="p">:</span>                           <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;apps&quot;</span><span class="p">),</span>

           <span class="s">&quot;paste&quot;</span><span class="p">:</span>                            <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-v&quot;</span><span class="p">),</span>
           <span class="s">&quot;duplicate &lt;n&gt;&quot;</span><span class="p">:</span>                    <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-c, c-v:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;copy&quot;</span><span class="p">:</span>                             <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-c&quot;</span><span class="p">),</span>
           <span class="s">&quot;cut&quot;</span><span class="p">:</span>                              <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-x&quot;</span><span class="p">),</span>
           <span class="s">&quot;select all&quot;</span><span class="p">:</span>                       <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-a&quot;</span><span class="p">),</span>
           <span class="s">&quot;[hold] shift&quot;</span><span class="p">:</span>                     <span class="n">Key</span><span class="p">(</span><span class="s">&quot;shift:down&quot;</span><span class="p">),</span>
           <span class="s">&quot;release shift&quot;</span><span class="p">:</span>                    <span class="n">Key</span><span class="p">(</span><span class="s">&quot;shift:up&quot;</span><span class="p">),</span>
           <span class="s">&quot;[hold] control&quot;</span><span class="p">:</span>                   <span class="n">Key</span><span class="p">(</span><span class="s">&quot;ctrl:down&quot;</span><span class="p">),</span>
           <span class="s">&quot;release control&quot;</span><span class="p">:</span>                  <span class="n">Key</span><span class="p">(</span><span class="s">&quot;ctrl:up&quot;</span><span class="p">),</span>
           <span class="s">&quot;release [all]&quot;</span><span class="p">:</span>                    <span class="n">release</span><span class="p">,</span>

           <span class="s">&quot;say &lt;text&gt;&quot;</span><span class="p">:</span>                       <span class="n">release</span> <span class="o">+</span> <span class="n">Text</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%(text)s</span><span class="s">&quot;</span><span class="p">),</span>
           <span class="s">&quot;mimic &lt;text&gt;&quot;</span><span class="p">:</span>                     <span class="n">release</span> <span class="o">+</span> <span class="n">Mimic</span><span class="p">(</span><span class="n">extra</span><span class="o">=</span><span class="s">&quot;text&quot;</span><span class="p">),</span>
          <span class="p">}</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we define various functions for formatting text.</span>
<span class="c"># Each of these functions must have a docstring which defines its</span>
<span class="c">#  spoken-form.  This docstring must include the &quot;&lt;dictation&gt;&quot; extra.</span>
<span class="c">#  See below for various examples.</span>

<span class="c"># Format: some_words</span>
<span class="k">def</span> <span class="nf">format_score</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>          <span class="c"># Function name must start with &quot;format_&quot;.</span>
    <span class="sd">&quot;&quot;&quot; score &lt;dictation&gt; &quot;&quot;&quot;</span>         <span class="c"># Docstring defining spoken-form.</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>             <span class="c"># Get written-form of dictated text.</span>
    <span class="k">return</span> <span class="s">&quot;_&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">))</span>  <span class="c"># Put underscores between words.</span>

<span class="c"># Format: some_words()</span>
<span class="k">def</span> <span class="nf">format_under_function</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; under func &lt;dictation&gt; &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
    <span class="k">return</span> <span class="s">&quot;_&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">))</span> <span class="o">+</span> <span class="s">&quot;()&quot;</span>

<span class="c"># Format: SomeWords</span>
<span class="k">def</span> <span class="nf">format_studley</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; studley &lt;dictation&gt; &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="n">word</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span> <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)]</span>
    <span class="k">return</span> <span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>

<span class="c"># Format: somewords</span>
<span class="k">def</span> <span class="nf">format_one_word</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; [all] one word &lt;dictation&gt; &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
    <span class="k">return</span> <span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">))</span>

<span class="c"># Format: SOMEWORDS</span>
<span class="k">def</span> <span class="nf">format_upper_one_word</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; one word upper &lt;dictation&gt; &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="n">word</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)]</span>
    <span class="k">return</span> <span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>

<span class="c"># Format: SOME_WORDS</span>
<span class="k">def</span> <span class="nf">format_upper_score</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; upper score &lt;dictation&gt; &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="n">word</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)]</span>
    <span class="k">return</span> <span class="s">&quot;_&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>

<span class="c"># Format: someWords</span>
<span class="k">def</span> <span class="nf">format_java_method</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Java method &lt;dictation&gt; &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">words</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span><span class="p">[</span><span class="mf">1</span><span class="p">:])</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="module-source-code">
<h2>Module source code<a class="headerlink" href="#module-source-code" title="Permalink to this headline">¶</a></h2>
<p>The most current version of this module can be downloaded from the <a class="reference external" href="http://dragonfly-modules.googlecode.com/svn/trunk/command-modules/_multiedit.py">repository here</a>.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># This file is a command-module for Dragonfly.</span>
<span class="c"># (c) Copyright 2008 by Christo Butcher</span>
<span class="c"># Licensed under the LGPL, see &lt;http://www.gnu.org/licenses/&gt;</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Command-module for cursor movement and **editing**</span>
<span class="sd">============================================================================</span>

<span class="sd">This module allows the user to control the cursor and</span>
<span class="sd">efficiently perform multiple text editing actions within a</span>
<span class="sd">single phrase.</span>


<span class="sd">Example commands</span>
<span class="sd">----------------------------------------------------------------------------</span>

<span class="sd">*Note the &quot;/&quot; characters in the examples below are simply</span>
<span class="sd">to help the reader see the different parts of each voice</span>
<span class="sd">command.  They are not present in the actual command and</span>
<span class="sd">should not be spoken.*</span>

<span class="sd">Example: **&quot;up 4 / down 1 page / home / space 2&quot;**</span>
<span class="sd">   This command will move the cursor up 4 lines, down 1 page,</span>
<span class="sd">   move to the beginning of the line, and then insert 2 spaces.</span>

<span class="sd">Example: **&quot;left 7 words / backspace 3 / insert hello Cap world&quot;**</span>
<span class="sd">   This command will move the cursor left 7 words, then delete</span>
<span class="sd">   the 3 characters before the cursor, and finally insert</span>
<span class="sd">   the text &quot;hello World&quot;.</span>

<span class="sd">Example: **&quot;home / space 4 / down / 43 times&quot;**</span>
<span class="sd">   This command will insert 4 spaces at the beginning of</span>
<span class="sd">   of this and the next 42 lines.  The final &quot;43 times&quot;</span>
<span class="sd">   repeats everything in front of it that many times.</span>


<span class="sd">Discussion of this module</span>
<span class="sd">----------------------------------------------------------------------------</span>

<span class="sd">This command-module creates a powerful voice command for</span>
<span class="sd">editing and cursor movement.  This command&#39;s structure can</span>
<span class="sd">be represented by the following simplified language model:</span>

<span class="sd"> - *CommandRule* -- top-level rule which the user can say</span>
<span class="sd">    - *repetition* -- sequence of actions (name = &quot;sequence&quot;)</span>
<span class="sd">       - *KeystrokeRule* -- rule that maps a single</span>
<span class="sd">         spoken-form to an action</span>
<span class="sd">    - *optional* -- optional specification of repeat count</span>
<span class="sd">       - *integer* -- repeat count (name = &quot;n&quot;)</span>
<span class="sd">       - *literal* -- &quot;times&quot;</span>

<span class="sd">The top-level command rule has a callback method which is</span>
<span class="sd">called when this voice command is recognized.  The logic</span>
<span class="sd">within this callback is very simple:</span>

<span class="sd">1. Retrieve the sequence of actions from the element with</span>
<span class="sd">   the name &quot;sequence&quot;.</span>
<span class="sd">2. Retrieve the repeat count from the element with the name</span>
<span class="sd">   &quot;n&quot;.</span>
<span class="sd">3. Execute the actions the specified number of times.</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">pkg_resources</span>
    <span class="n">pkg_resources</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="s">&quot;dragonfly &gt;= 0.6.5beta1.dev-r99&quot;</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>

<span class="kn">from</span> <span class="nn">dragonfly</span> <span class="kn">import</span> <span class="o">*</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we globally defined the release action which releases all</span>
<span class="c">#  modifier-keys used within this grammar.  It is defined here</span>
<span class="c">#  because this functionality is used in many different places.</span>
<span class="c">#  Note that it is harmless to release (&quot;...:up&quot;) a key multiple</span>
<span class="c">#  times or when that key is not held down at all.</span>

<span class="n">release</span> <span class="o">=</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;shift:up, ctrl:up&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Set up this module&#39;s configuration.</span>

<span class="n">config</span>            <span class="o">=</span> <span class="n">Config</span><span class="p">(</span><span class="s">&quot;multi edit&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">cmd</span>        <span class="o">=</span> <span class="n">Section</span><span class="p">(</span><span class="s">&quot;Language section&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">cmd</span><span class="o">.</span><span class="n">map</span>    <span class="o">=</span> <span class="n">Item</span><span class="p">(</span>
    <span class="c"># Here we define the *default* command map.  If you would like to</span>
    <span class="c">#  modify it to your personal taste, please *do not* make changes</span>
    <span class="c">#  here.  Instead change the *config file* called &quot;_multiedit.txt&quot;.</span>
    <span class="p">{</span>
     <span class="c"># Spoken-form    -&gt;    -&gt;    -&gt;     Action object</span>
     <span class="s">&quot;up [&lt;n&gt;]&quot;</span><span class="p">:</span>                         <span class="n">Key</span><span class="p">(</span><span class="s">&quot;up:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;down [&lt;n&gt;]&quot;</span><span class="p">:</span>                       <span class="n">Key</span><span class="p">(</span><span class="s">&quot;down:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;left [&lt;n&gt;]&quot;</span><span class="p">:</span>                       <span class="n">Key</span><span class="p">(</span><span class="s">&quot;left:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;right [&lt;n&gt;]&quot;</span><span class="p">:</span>                      <span class="n">Key</span><span class="p">(</span><span class="s">&quot;right:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;page up [&lt;n&gt;]&quot;</span><span class="p">:</span>                    <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgup:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;page down [&lt;n&gt;]&quot;</span><span class="p">:</span>                  <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgdown:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;up &lt;n&gt; (page | pages)&quot;</span><span class="p">:</span>            <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgup:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;down &lt;n&gt; (page | pages)&quot;</span><span class="p">:</span>          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;pgdown:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;left &lt;n&gt; (word | words)&quot;</span><span class="p">:</span>          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-left:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;right &lt;n&gt; (word | words)&quot;</span><span class="p">:</span>         <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-right:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;home&quot;</span><span class="p">:</span>                             <span class="n">Key</span><span class="p">(</span><span class="s">&quot;home&quot;</span><span class="p">),</span>
     <span class="s">&quot;end&quot;</span><span class="p">:</span>                              <span class="n">Key</span><span class="p">(</span><span class="s">&quot;end&quot;</span><span class="p">),</span>
     <span class="s">&quot;doc home&quot;</span><span class="p">:</span>                         <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-home&quot;</span><span class="p">),</span>
     <span class="s">&quot;doc end&quot;</span><span class="p">:</span>                          <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-end&quot;</span><span class="p">),</span>

     <span class="s">&quot;space [&lt;n&gt;]&quot;</span><span class="p">:</span>                      <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;space:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;enter [&lt;n&gt;]&quot;</span><span class="p">:</span>                      <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;enter:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;tab [&lt;n&gt;]&quot;</span><span class="p">:</span>                        <span class="n">Key</span><span class="p">(</span><span class="s">&quot;tab:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;delete [&lt;n&gt;]&quot;</span><span class="p">:</span>                     <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;del:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;delete [&lt;n&gt; | this] (line|lines)&quot;</span><span class="p">:</span> <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;home, s-down:</span><span class="si">%(n)d</span><span class="s">, del&quot;</span><span class="p">),</span>
     <span class="s">&quot;backspace [&lt;n&gt;]&quot;</span><span class="p">:</span>                  <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;backspace:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;pop up&quot;</span><span class="p">:</span>                           <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;apps&quot;</span><span class="p">),</span>

     <span class="s">&quot;paste&quot;</span><span class="p">:</span>                            <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-v&quot;</span><span class="p">),</span>
     <span class="s">&quot;duplicate &lt;n&gt;&quot;</span><span class="p">:</span>                    <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-c, c-v:</span><span class="si">%(n)d</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;copy&quot;</span><span class="p">:</span>                             <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-c&quot;</span><span class="p">),</span>
     <span class="s">&quot;cut&quot;</span><span class="p">:</span>                              <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-x&quot;</span><span class="p">),</span>
     <span class="s">&quot;select all&quot;</span><span class="p">:</span>                       <span class="n">release</span> <span class="o">+</span> <span class="n">Key</span><span class="p">(</span><span class="s">&quot;c-a&quot;</span><span class="p">),</span>
     <span class="s">&quot;[hold] shift&quot;</span><span class="p">:</span>                     <span class="n">Key</span><span class="p">(</span><span class="s">&quot;shift:down&quot;</span><span class="p">),</span>
     <span class="s">&quot;release shift&quot;</span><span class="p">:</span>                    <span class="n">Key</span><span class="p">(</span><span class="s">&quot;shift:up&quot;</span><span class="p">),</span>
     <span class="s">&quot;[hold] control&quot;</span><span class="p">:</span>                   <span class="n">Key</span><span class="p">(</span><span class="s">&quot;ctrl:down&quot;</span><span class="p">),</span>
     <span class="s">&quot;release control&quot;</span><span class="p">:</span>                  <span class="n">Key</span><span class="p">(</span><span class="s">&quot;ctrl:up&quot;</span><span class="p">),</span>
     <span class="s">&quot;release [all]&quot;</span><span class="p">:</span>                    <span class="n">release</span><span class="p">,</span>

     <span class="s">&quot;say &lt;text&gt;&quot;</span><span class="p">:</span>                       <span class="n">release</span> <span class="o">+</span> <span class="n">Text</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%(text)s</span><span class="s">&quot;</span><span class="p">),</span>
     <span class="s">&quot;mimic &lt;text&gt;&quot;</span><span class="p">:</span>                     <span class="n">release</span> <span class="o">+</span> <span class="n">Mimic</span><span class="p">(</span><span class="n">extra</span><span class="o">=</span><span class="s">&quot;text&quot;</span><span class="p">),</span>
    <span class="p">},</span>
    <span class="n">namespace</span><span class="o">=</span><span class="p">{</span>
     <span class="s">&quot;Key&quot;</span><span class="p">:</span>   <span class="n">Key</span><span class="p">,</span>
     <span class="s">&quot;Text&quot;</span><span class="p">:</span>  <span class="n">Text</span><span class="p">,</span>
    <span class="p">}</span>
<span class="p">)</span>
<span class="n">namespace</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>

<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we prepare the list of formatting functions from the config file.</span>

<span class="c"># Retrieve text-formatting functions from this module&#39;s config file.</span>
<span class="c">#  Each of these functions must have a name that starts with &quot;format_&quot;.</span>
<span class="n">format_functions</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">if</span> <span class="n">namespace</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span> <span class="ow">in</span> <span class="n">namespace</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
     <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;format_&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">callable</span><span class="p">(</span><span class="n">function</span><span class="p">):</span>
        <span class="n">spoken_form</span> <span class="o">=</span> <span class="n">function</span><span class="o">.</span><span class="n">__doc__</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

        <span class="c"># We wrap generation of the Function action in a function so</span>
        <span class="c">#  that its *function* variable will be local.  Otherwise it</span>
        <span class="c">#  would change during the next iteration of the namespace loop.</span>
        <span class="k">def</span> <span class="nf">wrap_function</span><span class="p">(</span><span class="n">function</span><span class="p">):</span>
            <span class="k">def</span> <span class="nf">_function</span><span class="p">(</span><span class="n">dictation</span><span class="p">):</span>
                <span class="n">formatted_text</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">dictation</span><span class="p">)</span>
                <span class="n">Text</span><span class="p">(</span><span class="n">formatted_text</span><span class="p">)</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">Function</span><span class="p">(</span><span class="n">_function</span><span class="p">)</span>

        <span class="n">action</span> <span class="o">=</span> <span class="n">wrap_function</span><span class="p">(</span><span class="n">function</span><span class="p">)</span>
        <span class="n">format_functions</span><span class="p">[</span><span class="n">spoken_form</span><span class="p">]</span> <span class="o">=</span> <span class="n">action</span>


<span class="c"># Here we define the text formatting rule.</span>
<span class="c"># The contents of this rule were built up from the &quot;format_*&quot;</span>
<span class="c">#  functions in this module&#39;s config file.</span>
<span class="k">if</span> <span class="n">format_functions</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">FormatRule</span><span class="p">(</span><span class="n">MappingRule</span><span class="p">):</span>

        <span class="n">mapping</span>  <span class="o">=</span> <span class="n">format_functions</span>
        <span class="n">extras</span>   <span class="o">=</span> <span class="p">[</span><span class="n">Dictation</span><span class="p">(</span><span class="s">&quot;dictation&quot;</span><span class="p">)]</span>

<span class="k">else</span><span class="p">:</span>
    <span class="n">FormatRule</span> <span class="o">=</span> <span class="bp">None</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we define the keystroke rule.</span>

<span class="c"># This rule maps spoken-forms to actions.  Some of these</span>
<span class="c">#  include special elements like the number with name &quot;n&quot;</span>
<span class="c">#  or the dictation with name &quot;text&quot;.  This rule is not</span>
<span class="c">#  exported, but is referenced by other elements later on.</span>
<span class="c">#  It is derived from MappingRule, so that its &quot;value&quot; when</span>
<span class="c">#  processing a recognition will be the right side of the</span>
<span class="c">#  mapping: an action.</span>
<span class="c"># Note that this rule does not execute these actions, it</span>
<span class="c">#  simply returns them when it&#39;s value() method is called.</span>
<span class="c">#  For example &quot;up 4&quot; will give the value Key(&quot;up:4&quot;).</span>
<span class="c"># More information about Key() actions can be found here:</span>
<span class="c">#  http://dragonfly.googlecode.com/svn/trunk/dragonfly/documentation/actionkey.html</span>
<span class="k">class</span> <span class="nc">KeystrokeRule</span><span class="p">(</span><span class="n">MappingRule</span><span class="p">):</span>

    <span class="n">exported</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="n">mapping</span>  <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">cmd</span><span class="o">.</span><span class="n">map</span>
    <span class="n">extras</span>   <span class="o">=</span> <span class="p">[</span>
                <span class="n">IntegerRef</span><span class="p">(</span><span class="s">&quot;n&quot;</span><span class="p">,</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">100</span><span class="p">),</span>
                <span class="n">Dictation</span><span class="p">(</span><span class="s">&quot;text&quot;</span><span class="p">),</span>
               <span class="p">]</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s">&quot;n&quot;</span><span class="p">:</span> <span class="mf">1</span><span class="p">,</span>
               <span class="p">}</span>
    <span class="c"># Note: when processing a recognition, the *value* of</span>
    <span class="c">#  this rule will be an action object from the right side</span>
    <span class="c">#  of the mapping given above.  This is default behavior</span>
    <span class="c">#  of the MappingRule class&#39; value() method.  It also</span>
    <span class="c">#  substitutes any &quot;%(...).&quot; within the action spec</span>
    <span class="c">#  with the appropriate spoken values.</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we create an element which is the sequence of keystrokes.</span>

<span class="c"># First we create an element that references the keystroke rule.</span>
<span class="c">#  Note: when processing a recognition, the *value* of this element</span>
<span class="c">#  will be the value of the referenced rule: an action.</span>
<span class="n">alternatives</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">alternatives</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">RuleRef</span><span class="p">(</span><span class="n">rule</span><span class="o">=</span><span class="n">KeystrokeRule</span><span class="p">()))</span>
<span class="k">if</span> <span class="n">FormatRule</span><span class="p">:</span>
    <span class="n">alternatives</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">RuleRef</span><span class="p">(</span><span class="n">rule</span><span class="o">=</span><span class="n">FormatRule</span><span class="p">()))</span>
<span class="n">single_action</span> <span class="o">=</span> <span class="n">Alternative</span><span class="p">(</span><span class="n">alternatives</span><span class="p">)</span>

<span class="c"># Second we create a repetition of keystroke elements.</span>
<span class="c">#  This element will match anywhere between 1 and 16 repetitions</span>
<span class="c">#  of the keystroke elements.  Note that we give this element</span>
<span class="c">#  the name &quot;sequence&quot; so that it can be used as an extra in</span>
<span class="c">#  the rule definition below.</span>
<span class="c"># Note: when processing a recognition, the *value* of this element</span>
<span class="c">#  will be a sequence of the contained elements: a sequence of</span>
<span class="c">#  actions.</span>
<span class="n">sequence</span> <span class="o">=</span> <span class="n">Repetition</span><span class="p">(</span><span class="n">single_action</span><span class="p">,</span> <span class="nb">min</span><span class="o">=</span><span class="mf">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mf">16</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;sequence&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Here we define the top-level rule which the user can say.</span>

<span class="c"># This is the rule that actually handles recognitions.</span>
<span class="c">#  When a recognition occurs, it&#39;s _process_recognition()</span>
<span class="c">#  method will be called.  It receives information about the</span>
<span class="c">#  recognition in the &quot;extras&quot; argument: the sequence of</span>
<span class="c">#  actions and the number of times to repeat them.</span>
<span class="k">class</span> <span class="nc">RepeatRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="c"># Here we define this rule&#39;s spoken-form and special elements.</span>
    <span class="n">spec</span>     <span class="o">=</span> <span class="s">&quot;&lt;sequence&gt; [[[and] repeat [that]] &lt;n&gt; times]&quot;</span>
    <span class="n">extras</span>   <span class="o">=</span> <span class="p">[</span>
                <span class="n">sequence</span><span class="p">,</span>                 <span class="c"># Sequence of actions defined above.</span>
                <span class="n">IntegerRef</span><span class="p">(</span><span class="s">&quot;n&quot;</span><span class="p">,</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">100</span><span class="p">),</span>  <span class="c"># Times to repeat the sequence.</span>
               <span class="p">]</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s">&quot;n&quot;</span><span class="p">:</span> <span class="mf">1</span><span class="p">,</span>                   <span class="c"># Default repeat count.</span>
               <span class="p">}</span>

    <span class="c"># This method gets called when this rule is recognized.</span>
    <span class="c"># Arguments:</span>
    <span class="c">#  - node -- root node of the recognition parse tree.</span>
    <span class="c">#  - extras -- dict of the &quot;extras&quot; special elements:</span>
    <span class="c">#     . extras[&quot;sequence&quot;] gives the sequence of actions.</span>
    <span class="c">#     . extras[&quot;n&quot;] gives the repeat count.</span>
    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">sequence</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;sequence&quot;</span><span class="p">]</span>   <span class="c"># A sequence of actions.</span>
        <span class="n">count</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;n&quot;</span><span class="p">]</span>             <span class="c"># An integer repeat count.</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">count</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="n">sequence</span><span class="p">:</span>
                <span class="n">action</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>
        <span class="n">release</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Create and load this module&#39;s grammar.</span>

<span class="n">grammar</span> <span class="o">=</span> <span class="n">Grammar</span><span class="p">(</span><span class="s">&quot;multi edit&quot;</span><span class="p">)</span>   <span class="c"># Create this module&#39;s grammar.</span>
<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">RepeatRule</span><span class="p">())</span>    <span class="c"># Add the top-level rule.</span>
<span class="n">grammar</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>                    <span class="c"># Load the grammar.</span>

<span class="c"># Unload function which will be called at unload time.</span>
<span class="k">def</span> <span class="nf">unload</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">grammar</span>
    <span class="k">if</span> <span class="n">grammar</span><span class="p">:</span> <span class="n">grammar</span><span class="o">.</span><span class="n">unload</span><span class="p">()</span>
    <span class="n">grammar</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Command-module for cursor movement and <strong>editing</strong></a><ul>
<li><a class="reference external" href="#example-commands">Example commands</a></li>
<li><a class="reference external" href="#discussion-of-this-module">Discussion of this module</a></li>
<li><a class="reference external" href="#configuration-examples">Configuration examples</a><ul>
<li><a class="reference external" href="#configuration-multiedit-en-txt">Configuration &#8220;_multiedit-en.txt&#8221;</a></li>
</ul>
</li>
<li><a class="reference external" href="#module-source-code">Module source code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="mod-_winctrl.html" title="previous chapter">Command-module for moving and controlling <strong>windows</strong></a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mod-_cmdmemory.html" title="next chapter">Command-module for <strong>command memory</strong></a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/mod-_multiedit.txt">Show Source</a></li>
            </ul>
          <h3>Quick search</h3>
            <form class="search" action="search.html" method="get">
              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
              <input type="hidden" name="check_keywords" value="yes" />
              <input type="hidden" name="area" value="default" />
            </form>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_cmdmemory.html" title="Command-module for command memory"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-_winctrl.html" title="Command-module for moving and controlling windows"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Christo Butcher.
      Last updated on Apr 10, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.1.
    </div>
  </body>
</html>